#include "md_dtv.h"
#include "ui_md_dtv.h"

Md_dtv::Md_dtv(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Md_dtv)
{
    ui->setupUi(this);
}

Md_dtv::Md_dtv(TCL_MSTAR_DTV_TV *mstar,QList<uint>*ms_atv_num,QMap<uint,uchar>*ms_dtv_air_num,QMap<uint,uchar> *ms_dtv_cable_num,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Md_dtv)
{
    ui->setupUi(this);
    Q_CHECK_PTR(mstar);
    solution=1;
    mstar->Get_Data(&ch_data);
    atv_num=ms_atv_num;
    dtv_air_num=ms_dtv_air_num;
    dtv_cable_num=ms_dtv_cable_num;
    ms_dtv=mstar;
    My_Init();

}
Md_dtv::Md_dtv(TCL_MSTAR_DTV_TV *mstar,QMap<uint,uchar>*tv_air_num,QMap<uint,uchar> *tv_cable_num,QWidget *parent):
    QWidget(parent),
    ui(new Ui::Md_dtv)
{
    ui->setupUi(this);
    Q_CHECK_PTR(mstar);
    solution=1;
    mstar->Get_Data(&ch_data);
    dtv_air_num=tv_air_num;
    dtv_cable_num=tv_cable_num;
    atv_num=NULL;
    ms_dtv=mstar;
    My_Init();
}

Md_dtv::Md_dtv(TCL_MTK_DTV_TV *mtk,QMap<uint,uchar>*tv_air_num,QMap<uint,uchar> *tv_cable_num,QWidget *parent):
    QWidget(parent),
    ui(new Ui::Md_dtv)
{
    ui->setupUi(this);
    Q_CHECK_PTR(mtk);
    solution=0;
    mtk->Get_Data(&ch_data);
    dtv_air_num=tv_air_num;
    dtv_cable_num=tv_cable_num;
    atv_num=NULL;
    mt_dtv=mtk;
    My_Init();
}
Md_dtv::Md_dtv(TCL_MTK_DTV_TV *mtk,QList<uint>*mt_atv_num,QMap<uint,uchar> *mt_dtv_air_num,QMap<uint,uchar> *mt_dtv_cable_num, QWidget *parent):
    QWidget(parent),
    ui(new Ui::Md_dtv)
{
    ui->setupUi(this);
    Q_CHECK_PTR(mtk);
    solution=0;
    mtk->Get_Data(&ch_data);
    atv_num=mt_atv_num;
    dtv_air_num=mt_dtv_air_num;
    dtv_cable_num=mt_dtv_cable_num;
    mt_dtv=mtk;
    My_Init();

}

Md_dtv::~Md_dtv()
{
    delete ui;
    mt_dtv=NULL;
    ms_dtv=NULL;
    atv_num=NULL;
    dtv_air_num=NULL;
    dtv_cable_num=NULL;
}

void Md_dtv::My_Init()
{
    ui->msg->setStyleSheet("color:red");
    this->setStyleSheet("background-color:rgb(135,206,235)");
    ui->Item1->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item2->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item3->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item4->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item5->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item6->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->pB_Confirm->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->Item7->setStyleSheet("border:2px groove gray;border-radius:10px;padding:2px 4px;background-color:rgb(135,206,235)");
    ui->label->setText("Channel Number:");
    ui->Item1->setText(ch_data.at(0));
  //  connect(ui->Item1,SIGNAL(editingFinished()),this,SLOT(Check_Num()));
    ui->label_2->setText("Channel Name");
    ui->Item2->setText(ch_data.at(1));
    name=ch_data.at(1);
    ui->label_3->setText("Channel Class");

   ui->Item3->setText(ch_data.at(2));
    ui->Item3->setEnabled(false);
    QString tmp1="D";
    QString tmp2="T";
    QString tmp3="R";
    QString tmp4="U";
    if(0==ch_data.at(3).compare(tmp1))
        ch_type=1;
    else if(0==ch_data.at(3).compare(tmp2))
        ch_type=2;
    else if(0==ch_data.at(3).compare(tmp3))
        ch_type=3;
    else if(0==ch_data.at(3).compare(tmp4))
        ch_type=4;
    else
        ch_type=5;
    //ui->Item3->setText(ch_data.at(2));
   // ui->Item3->setEnabled(false);
      ui->label_4->setText("type");
      ui->Item4->setText(ch_data.at(3));

    ui->Item4->setEnabled(false);
    ui->label_5->setText("Frequency(KHZ):");
    ui->Item5->setText(ch_data.at(4));
   // ui->Item5->setEnabled(false);

    ui->label_6->setText("Favorite Flag:");
    QStringList data;
    tmp1="0";
    tmp2="1";
    data.append(tmp1);
    data.append(tmp2);
    ui->Item6->addItems(data);
    if(0==ch_data.at(5).compare(tmp1))
        ui->Item6->setCurrentIndex(0);
    else if(0==ch_data.at(5).compare(tmp2))
        ui->Item6->setCurrentIndex(1);
    else
        ui->Item6->setCurrentIndex(0);


    ui->label_7->setText("Skip Flag:");

    ui->Item7->addItems(data);
    if(0==ch_data.at(6).compare(tmp1))
        ui->Item7->setCurrentIndex(0);
    else if(0==ch_data.at(6).compare(tmp2))
        ui->Item7->setCurrentIndex(1);
    else
        ui->Item7->setCurrentIndex(0);
    bool flag=false;
    bool *ok=&flag;
    ch_val=ch_data.at(0).toUInt(ok);
    STRING_TRAN_ERROR_0;
    tmp1="DTV-T";
    tmp2="DTV-C";
    tmp3="DTV-S/S2";
    tmp4="DTV-T2";
    if(0==ch_data.at(2).compare(tmp1))
        ch_class=0;
    else if(0==ch_data.at(2).compare(tmp2))
        ch_class=1;
    else if(0==ch_data.at(2).compare(tmp2))
        ch_class=2;
    else
    {
        ch_class=3;
    }
    STRING_TRAN_ERROR_0;
   // ch_type=(uchar)ch_data.at(3).toUShort();

    connect(ui->pB_Confirm,SIGNAL(clicked()),this,SLOT(Confirm()));
    connect(ui->Item6,SIGNAL(activated(int)),this,SLOT(Set_Fav_Index(int)));
    connect(ui->Item7,SIGNAL(activated(int)),this,SLOT(Set_Skip_Index(int)));
    connect(ui->Item1,SIGNAL(editingFinished()),this,SLOT(Check_Num()));
    connect(ui->Item5,SIGNAL(editingFinished()),this,SLOT(Check_Freq()));
    connect(ui->Item2,SIGNAL(textChanged(QString)),this,SLOT(Set_Name(QString)));
    connect(ui->Item1,SIGNAL(textChanged(QString)),this,SLOT(Set_Num(QString)));
    connect(ui->Item5,SIGNAL(textChanged(QString)),this,SLOT(Set_Freq(QString)));
}


bool Md_dtv::is_close()
{

return true;
}




void Md_dtv::Set_Num(QString tmp)
{
    md_item.append(0);
}

void Md_dtv::Set_Fav_Index(int index)
{
    ui->Item6->setCurrentIndex(index);
    md_item.append(5);
}
void Md_dtv::Set_Name(QString tmp)
{
    //QString tmp1=ui->Item2->text();
   // ch_data.removeAt(1);
    //ch_data.insert(1,tmp);
    name=tmp;
    md_item.append(1);
}

void Md_dtv::Set_Skip_Index(int index)
{
    ui->Item7->setCurrentIndex(index);
    md_item.append(6);
}

void Md_dtv::Set_Freq(QString tmp)
{
    md_item.append(4);
}

void Md_dtv::Confirm()
{
    QString tmp;
    ch_data.clear();
    ch_data.append(ui->Item1->text());
    if(-1==Check_Num(ui->Item1->text().toUInt()))
        return ;
    ch_data.append(name);//针对编码不同的情况作的处理,如果频道名不是ASCII编写的，那么频道名处可能显示为空，
    QString tmp1="DTV-T";
    QString tmp2="DTV-C";
    QString tmp3="DTV-S/S2";
    //QString tmp4="DTV-T2";
    if(0==ui->Item3->text().compare(tmp1))
        tmp.setNum(0);
    else if(0==ui->Item3->text().compare(tmp2))
        tmp.setNum(1);
    else if(0==ui->Item3->text().compare(tmp3))
        tmp.setNum(2);
    else
        tmp.setNum(3);
    ch_data.append(tmp);

    tmp1="D";
    tmp2="R";
    tmp3="T";
    QString tmp4="U";
    QString tmp5=ui->Item4->text();
    if(0==tmp5.compare(tmp1))
           tmp.setNum(1);
    else if(0==tmp5.compare(tmp2))
        tmp.setNum(2);
    else if(0==tmp5.compare(tmp3))
        tmp.setNum(3);
    else if(0==tmp5.compare(tmp4))
        tmp.setNum(4);
    else
        tmp.setNum(5);//error happened


    ch_data.append(tmp);

    ch_data.append(ui->Item5->text());

    if(ui->Item6->currentIndex()==0)
        tmp.setNum(0);
    else if(ui->Item6->currentIndex()==1)
        tmp.setNum(1);
    else
        tmp.setNum(0);

    ch_data.append(tmp);
    if(ui->Item7->currentIndex()==0)
        tmp.setNum(0);
    else if(ui->Item7->currentIndex()==1)
        tmp.setNum(1);
    else
        tmp.setNum(0);
    ch_data.append(tmp);
    ui->msg->setText("success");
    if(1==solution)
    {
        ms_dtv->Set_Data(ch_data);
        emit Mstar_Close(*ms_dtv,md_item);

    }
    else if(0==solution)
    {
        mt_dtv->Set_Data(ch_data);
        emit Mtk_Close(*mt_dtv,md_item);
    }
    this->close();
}


int Md_dtv::Check_Num(uint num)
{
    uint type;
    QString tmp1="D";
    QString tmp2="R";
    QString tmp3="T";
    QString tmp4="U";
    if(0==ui->Item4->text().compare(tmp1))
        type=1;
    else if(0==ui->Item4->text().compare(tmp2))
        type=2;
    else if(0==ui->Item4->text().compare(tmp3))
        type=3;
    else if(0==ui->Item4->text().compare(tmp4))
        type=4;
    else
        type=5;//error happened
    //STRING_TRAN_ERROR_0;
    if(atv_num&&atv_num->size()>=0)
    {
        for(int i=0;i<atv_num->size();i++)
        {
            if(num==atv_num->at(i))
            {
                //QMessageBox::warning(this,"Warning!","The Channel Number ,which you modfied,is repeated with other atv channels.",QMessageBox::Ok);
                ui->msg->setText("The Channel Number ,which you modfied,is repeated with other atv channels.");
                ui->Item1->setText(ch_data.at(0));
                ui->pB_Confirm->setEnabled(false);
                return -1;
            }
        }
    }

    if((solution==1&&ch_class==0&&dtv_air_num)||(solution==0&&dtv_air_num))
    {
        if(dtv_air_num->size()>0)
        {
            QMap<uint,uchar>::iterator i;
            //QList<uchar> type_list;
            for(i=dtv_air_num->begin();i!=dtv_air_num->end();++i)
            {
                uint ntmp=i.key();
                uchar ctmp=i.value();
                if(num==ntmp&&num!=ch_val&&type==ctmp)
                {
                    //QMessageBox::warning(this,"Warning!","The Channel Number ,which you modfied,is repeated with other dtv channels.",QMessageBox::Ok);
                    ui->msg->setText("The Channel Number ,which you modfied,is repeated with other dtv channels.");
                    ui->Item1->setText(ch_data.at(0));
                    ui->pB_Confirm->setEnabled(false);
                    return -1;
                }
            }
        }else
        {
            //QMessageBox::information(this,"error","There is error happened where QList dtv is NULL or its size below zero!");
            ui->msg->setText("There is an error happened where QList dtv is NULL or its size below zero!");
            ui->pB_Confirm->setEnabled(false);
            return -1;
        }

        QMap<uint,uchar>::iterator i;
        for(i=dtv_air_num->begin();i!=dtv_air_num->end();i++)
            {
            if(ch_val==i.key()&&ch_type==i.value())
                {

                   dtv_air_num->insertMulti(num,i.value());
                   dtv_air_num->erase(i);
                   break;
                }
            }
      /*  qDebug("Modify Data:");
        for(i=dtv_air_num->begin();i!=dtv_air_num->end();i++)
            qDebug("ch_val: %d",i.key());*/
    }else if(ch_class==1&&dtv_cable_num&&solution==1)
    {
        if(dtv_cable_num->size()>0)
        {
            QMap<uint,uchar>::const_iterator i;
            //QList<uchar> type_list;
            for(i=dtv_cable_num->constBegin();i!=dtv_cable_num->constEnd();++i)
            {
                if(num==i.key()&&num!=ch_val&&type==i.value())
                {
                    //QMessageBox::warning(this,"Warning!","The Channel Number ,which you modfied,is repeated with other dtv channels.",QMessageBox::Ok);
                    ui->msg->setText("The Channel Number ,which you modfied,is repeated with other dtv channels.");
                    ui->Item1->setText(ch_data.at(0));
                    ui->pB_Confirm->setEnabled(false);
                    return -1;
                }
            }
        }else
        {
           // QMessageBox::information(this,"error","There is error happened where QList dtv is NULL or its size below zero!");
            ui->msg->setText("There is a error happened where QList dtv is NULL or its size below zero!");
            ui->pB_Confirm->setEnabled(false);
           return -1;
        }

        QMap<uint,uchar>::iterator i;
        for(i=dtv_cable_num->begin();i!=dtv_cable_num->end();i++)
            {
            if(ch_val==i.key()&&ch_type==i.value())
                {
                   dtv_cable_num->insertMulti(num,i.value());
                   dtv_cable_num->erase(i);
                   break;
                }
            }
    }else
    {
        QMessageBox::warning(this,"error","QList DTV is NULL or the dtv data ,which you chose,its class out of range! ");
        ui->pB_Confirm->setEnabled(false);
        return -1 ;
    }
}

void Md_dtv::Check_Num()
{
    CHECK_INPUT_EMPTY(ui->Item1->text());
    ui->msg->clear();
    bool flag=false;
    bool *ok=&flag;
    ui->Item1->text().toUInt(ok);
    if(*ok==false)
    {
        ui->msg->setText("Channel Number Error");
        ui->pB_Confirm->setEnabled(false);
        return ;
    }else{
        ui->pB_Confirm->setEnabled(true);
    }
   /* uint type;
    QString tmp1="D";
    QString tmp2="R";
    QString tmp3="T";
    QString tmp4="U";
    if(0==ui->Item4->text().compare(tmp1))
        type=1;
    else if(0==ui->Item4->text().compare(tmp2))
        type=2;
    else if(0==ui->Item4->text().compare(tmp3))
        type=3;
    else if(0==ui->Item4->text().compare(tmp4))
        type=4;
    else
        type=5;//error happened
    //STRING_TRAN_ERROR_0;
    if(atv_num&&atv_num->size()>=0)
    {
        for(int i=0;i<atv_num->size();i++)
        {
            if(num==atv_num->at(i))
            {
                //QMessageBox::warning(this,"Warning!","The Channel Number ,which you modfied,is repeated with other atv channels.",QMessageBox::Ok);
                ui->msg->setText("The Channel Number ,which you modfied,is repeated with other atv channels.");
                ui->Item1->setText(ch_data.at(0));
                ui->pB_Confirm->setEnabled(false);
                return ;
            }
        }
    }

    if((solution==1&&ch_class==0&&dtv_air_num)||(solution==0&&dtv_air_num))
    {
        if(dtv_air_num->size()>0)
        {
            QMap<uint,uchar>::iterator i;
            //QList<uchar> type_list;
            for(i=dtv_air_num->begin();i!=dtv_air_num->end();++i)
            {
                uint ntmp=i.key();
                uchar ctmp=i.value();
                if(num==ntmp&&num!=ch_val&&type==ctmp)
                {
                    //QMessageBox::warning(this,"Warning!","The Channel Number ,which you modfied,is repeated with other dtv channels.",QMessageBox::Ok);
                    ui->msg->setText("The Channel Number ,which you modfied,is repeated with other dtv channels.");
                    ui->Item1->setText(ch_data.at(0));
                    ui->pB_Confirm->setEnabled(false);
                    return ;
                }
            }
        }else
        {
            //QMessageBox::information(this,"error","There is error happened where QList dtv is NULL or its size below zero!");
            ui->msg->setText("There is an error happened where QList dtv is NULL or its size below zero!");
            ui->pB_Confirm->setEnabled(false);
            exit(-1);
        }

        QMap<uint,uchar>::iterator i;
        for(i=dtv_air_num->begin();i!=dtv_air_num->end();i++)
            {
            if(ch_val==i.key()&&ch_type==i.value())
                {

                   dtv_air_num->insertMulti(num,i.value());
                   dtv_air_num->erase(i);
                   break;
                }
            }
        qDebug("Modify Data:");
        for(i=dtv_air_num->begin();i!=dtv_air_num->end();i++)
            qDebug("ch_val: %d",i.key());
    }else if(ch_class==1&&dtv_cable_num&&solution==1)
    {
        if(dtv_cable_num->size()>0)
        {
            QMap<uint,uchar>::const_iterator i;
            //QList<uchar> type_list;
            for(i=dtv_cable_num->constBegin();i!=dtv_cable_num->constEnd();++i)
            {
                if(num==i.key()&&num!=ch_val&&type==i.value())
                {
                    //QMessageBox::warning(this,"Warning!","The Channel Number ,which you modfied,is repeated with other dtv channels.",QMessageBox::Ok);
                    ui->msg->setText("The Channel Number ,which you modfied,is repeated with other dtv channels.");
                    ui->Item1->setText(ch_data.at(0));
                    ui->pB_Confirm->setEnabled(false);
                    return ;
                }
            }
        }else
        {
           // QMessageBox::information(this,"error","There is error happened where QList dtv is NULL or its size below zero!");
            ui->msg->setText("There is a error happened where QList dtv is NULL or its size below zero!");
            ui->pB_Confirm->setEnabled(false);
            exit(-1);
        }

        QMap<uint,uchar>::iterator i;
        for(i=dtv_cable_num->begin();i!=dtv_cable_num->end();i++)
            {
            if(ch_val==i.key()&&ch_type==i.value())
                {
                   dtv_cable_num->insertMulti(num,i.value());
                   dtv_cable_num->erase(i);
                   break;
                }
            }
    }else
    {
        QMessageBox::warning(this,"error","QList DTV is NULL or the dtv data ,which you chose,its class out of range! ");
        ui->pB_Confirm->setEnabled(false);
        exit(-1) ;
    }*/
    ui->pB_Confirm->setEnabled(true);

}

void Md_dtv::Check_Freq()
{
    CHECK_INPUT_EMPTY(ui->Item5->text());
    bool Falg=false;
    bool *ok=&Falg;
    ui->Item5->text().toInt(ok);
    if(*ok==false)
    {
        ui->msg->setText("Frequency Error");
        ui->pB_Confirm->setEnabled(false);
    }else{
        ui->msg->clear();
        ui->pB_Confirm->setEnabled(true);
    }

}
